JsAccessible

This annotation allows exposing methods and fields of an injected Java object to JavaScript.

You can make accessible only public types, methods, and fields. The full list of the supported cases is the following:

  • A top-level class or an interface.
  • A nested static class or an interface.
  • A non-static method of a class or an interface.
  • A non-static field of a class.

The annotation cannot be applied to non-public types, methods, and fields. Public methods and fields of a non-public type are considered non-public.

When you annotate a type, all its public methods and fields become accessible to JavaScript. When you annotate a method or a field of an unannotated type, only the annotated member becomes accessible to JavaScript.

An accessible method remains so when it is overridden in a subclass. It means that you can make an interface accessible and pass any of its implementations to JavaScript: all the methods declared in the interface will be accessible from JavaScript. Other methods and fields declared in the implementing class will remain inaccessible unless you explicitly mark them or the entire type with this annotation.

Another way to make a type accessible from JavaScript is using JsAccessibleTypes. This is particularly useful when you want to make accessible one of the core Java types (e.g. java.util.List), or a type from a third-party library that you cannot make accessible using this annotation.

Examples:

Annotated methods and fields of a public top level class are accessible:

public final class TopClass {
    @JsAccessible
    public Object accessibleField;
    @JsAccessible
    public void accessibleMethod() {}
}

Annotated methods and fields of a public static nested class are accessible:

public final class TopClass {
    public static class NestedClass {
        @JsAccessible
        public Object accessibleField;
        @JsAccessible
        public void accessibleMethod() {}
    }
}

Unannotated methods and fields of an annotated class are accessible:

@JsAccessible
public final class TopClass {
    public Object accessibleField;
    public void accessibleMethod() {}
}

Methods and fields of a base annotated class are accessible from inheritors:

public final class TopClass {
    @JsAccessible
    public static class BaseNestedClass {
        public Object accessibleFieldFromInheritor;
        public void accessibleMethodFromInheritor() {}
    }
    public static class NestedClass extends BaseNestedClass {
        public Object inaccessibleField;
        public void inaccessibleMethod() {}
    }
}

Inherited methods and fields are not accessible if they or the class they are declared in are not annotated:

public final class TopClass {
    public static class BaseNestedClass {
        public Object inaccessibleField;
        public void inaccessibleMethod() {}
    }
    @JsAccessible
    public static class NestedClass extends BaseNestedClass {
        public Object accessibleField;
        public void accessibleMethod() {}
    }
}

Overridden class methods are accessible:

public final class TopClass {
    public static class BaseNestedClass {
        @JsAccessible
        public void method() {}
    }
    public static class NestedClass extends BaseNestedClass {
        @Override
        public void method() {} // accessible
    }
}

Implemented interface methods are accessible:

public static class TopClass {
    public interface NestedInterface {
        @JsAccessible
        void method();
    }
    public static class AccessibleImplementor implements NestedInterface {
        @Override
        public void method() { } // accessible
    }
}

If the signature of an accessible Java method has a primitive numeric parameter, then the number transferred from JavaScript will be checked for the possibility of converting to the Java parameter type. If the conversion can be performed without data loss and no other suitable overloaded methods were found, the method will be invoked.

If more than one method that can accept the passed parameters does exist, JavaScript throws an exception to indicate that the requested method call is ambiguous and cannot be performed.

If no methods or fields that correspond to the requested name found, JavaScript throws an exception indicating that the requested member does not exist.

If both method and field with the same name requested by JavaScript do exist, JavaScript throws an exception to indicate that the requested member is ambiguous and cannot be accessed.

Functions

Link copied to clipboard
abstract fun annotationType(): Class<out Annotation>
Link copied to clipboard
abstract fun equals(p: Any): Boolean
Link copied to clipboard
abstract fun hashCode(): Int
Link copied to clipboard
abstract fun toString(): String